// *** LICENSE HEADER ***
// Filename: ./roster.h
// This file is part of Nodewarz 0.1
// Nodewarz is a 3d OpenGL strategy game
// Copyright (C) 2007 CZestmyr
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// *** END LICENSE HEADER ***
#ifndef NODEWARS_ROSTER_H
#define NODEWARS_ROSTER_H

#include <SDL.h>
#include <SDL_net.h>
#include <GL/gl.h>
#include <cstdio>
#include <cstring>
#include "engine.h"
#include "fonts.h"
#include "playerlist.h"
#include "player.h"
#include "Ieventhandler.h"
#include "gui.h"
#include "buttonids.h"
#include "gamestate.h"
#include "connector.h"
#include "colorselect.h"
#include "definitions.h"

class engine;
class playerList;
class gui;

class roster: public IeventHandler {
    public:
        roster(engine* newParent);
        ~roster();

        //Init and deinit functions for creation and joining to the game
        //Basically they update the roster GUI
        void Init();
        void InitJoin();
        void DeinitJoin();
        void InitCreate();
        void DeinitCreate();
        void InitJoined();
        void DeinitJoined();
        void InitCreated();
        void DeinitCreated();

        //Creates a new player list
        void NewPlayerList();
        //Deletes the current player list
        void DeletePlayerList();
        //Returns the player list pointer
        playerList* GetPlayerList();

        //Adds a new player
        void AddPlayer();
        //Removes one player
        void RemovePlayer();
        //Updates player information labels when (in/de)crementing the player number
        void UpdatePlayers(bool inc);
        //Update all player information labels
        void UpdatePlayers();
        //Updates the player control labels
        void UpdatePlayerControls();

        //Updates kick buttons
        void UpdateKickButtons(bool*);
        //Updates the player control labels
        void UpdatePlayerLabels(); 
        //Updates the player color data
        void UpdatePlayerColorData();
        //Sends the player data to clients
        void SendPlayerData();

        //Client methods:

        //Generates labels for the given number of clients
        void GenerateClientPlayerLabels(int playernum);
        //Sets data for the given client
        void SetClientPlayerData(int playernum, int control, int r, int g, int b, const char* str);
        //Sets the client player list according to the GUI labels
        void SetClientPlayerList();

        //Toggles between create/join game
        void ChangeMode();
        //Draw the roster gui
        void Draw();

        //Wait for response from the server
        void WaitForResponse();
        //Update the roster
        void Update(int dt);

        //Return pointer to the GUI
        gui* GetGui();

        //IeventHandler methods
        void MouseClickEvent(int xCoordinate, int yCoordinate, bool right = false);
        void MouseReleaseEvent(int xCoordinate, int yCoordinate, bool right = false);
        void MouseMoveEvent(int newX, int newY, int relX, int relY) {}
        void KeyPressEvent(SDLKey key, char c);
        void KeyReleaseEvent(SDLKey key) {}
    private:
        engine* parent;
        playerList* thePlayerList;
        gui* theButtons;

        //Removes gui elements for all possible players
        void RemovePlayerGui(int num = -1);

        //server
        int timeToSendPlayerData;

        //client
        int lastPlayerNumber;

        bool waitingForResponse;
        bool connected;
};

#endif
